package dgut.bbm.service.impl;


import dgut.bbm.dao.GoodsPicDao;
import dgut.bbm.entity.GoodsPicEntity;
import dgut.bbm.to.GoodsSaveTo;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import dgut.bbm.common.utils.PageUtils;
import dgut.bbm.common.utils.Query;

import dgut.bbm.dao.GoodsDao;
import dgut.bbm.entity.GoodsEntity;
import dgut.bbm.service.GoodsService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {

    @Resource
    GoodsPicDao goodsPicDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<GoodsEntity> queryWrapper = new QueryWrapper<>();

        queryWrapper.and((wrapper) -> {
            wrapper.gt("stock", 0);
        });
        String name = (String) params.get("name");
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.and((wrapper) -> {
                wrapper.eq("name", name).or().like("name", name);
            });
        }

        String detail = (String) params.get("detail");
        if (!StringUtils.isEmpty(detail)) {
            queryWrapper.and((wrapper) -> {
                wrapper.eq("detail", detail).or().like("detail", detail);
            });
        }
        String min = (String) params.get("min");

        if (!StringUtils.isEmpty(min)) {
            queryWrapper.ge("price", min);
        }
        String statue = (String) params.get("statue");
        if (!StringUtils.isEmpty(statue)) {
            if (statue.equals("0"))
                queryWrapper.orderByDesc("create_time");
            if (statue.equals("1"))
                queryWrapper.orderByAsc("price");
            if (statue.equals("2"))
                queryWrapper.orderByDesc("price");
        }
        String max = (String) params.get("max");
        if (!StringUtils.isEmpty(max)) {
            try {
                BigDecimal bigDecimal = new BigDecimal(max);
                if (bigDecimal.compareTo(new BigDecimal("0")) == 1) {
                    queryWrapper.le("price", max);
                }
            } catch (Exception e) {

            }
        }
        IPage<GoodsEntity> page = this.page(new Query<GoodsEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public void saveGoods(GoodsSaveTo goodsSaveTo) {
        GoodsEntity goodsEntity = new GoodsEntity();
        BeanUtils.copyProperties(goodsSaveTo, goodsEntity);
        baseMapper.insert(goodsEntity);
        for (GoodsPicEntity picEntity : goodsSaveTo.getPictures()) {
            picEntity.setGoodsId(goodsEntity.getId());
            goodsPicDao.insert(picEntity);
        }
    }
    @Cacheable(value = {"Goods"},key = "#root.methodName + '_' + #id")
    @Override
    public List<GoodsPicEntity> listPic(Long id) {
        return goodsPicDao.selectList(new QueryWrapper<GoodsPicEntity>().eq("goods_id",id));
    }


    @Override
    public PageUtils pageByUserId(Map<String, Object> params) {
        QueryWrapper<GoodsEntity> queryWrapper = new QueryWrapper<>();

        String stock = (String) params.get("stock");
        if (!StringUtils.isEmpty(stock)) {
            if(stock.equals("0"))
                queryWrapper.and((wrapper) -> {
                    wrapper.le("stock", 0);
                });
            if(stock.equals("1"))
                queryWrapper.and((wrapper) -> {
                    wrapper.gt("stock", 0);
                });
        }
        String name = (String) params.get("name");
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.and((wrapper) -> {
                wrapper.eq("name", name).or().like("name", name);
            });
        }
        String userId = (String) params.get("userId");
        if (!StringUtils.isEmpty(userId)) {
            queryWrapper.and((wrapper) -> {
                wrapper.eq("user_id", userId).or().like("user_id", userId);
            });
        }
        String detail = (String) params.get("detail");
        if (!StringUtils.isEmpty(detail)) {
            queryWrapper.and((wrapper) -> {
                wrapper.eq("detail", detail).or().like("detail", detail);
            });
        }
        String min = (String) params.get("min");

        if (!StringUtils.isEmpty(min)) {
            queryWrapper.ge("price", min);
        }
        String statue = (String) params.get("statue");
        if (!StringUtils.isEmpty(statue)) {
            if (statue.equals("0"))
                queryWrapper.orderByDesc("create_time");
            if (statue.equals("1"))
                queryWrapper.orderByAsc("price");
            if (statue.equals("2"))
                queryWrapper.orderByDesc("price");
        }
        String max = (String) params.get("max");
        if (!StringUtils.isEmpty(max)) {
            try {
                BigDecimal bigDecimal = new BigDecimal(max);
                if (bigDecimal.compareTo(new BigDecimal("0")) == 1) {
                    queryWrapper.le("price", max);
                }
            } catch (Exception e) {

            }
        }
        IPage<GoodsEntity> page = this.page(new Query<GoodsEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);

    }


}